from TTS.api import TTS
from pydub import AudioSegment
from pydub.playback import play
import torch
import time
import os

# 1️⃣ 加载日语模型（Tacotron2 + HiFi-GAN vocoder）
tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=True)

# 自动选择 GPU 或 CPU
device = "cuda" if torch.cuda.is_available() else "cpu"
tts.to(device)

# 2️⃣ 日语单词列表
words = [
    "Hello, how are you?",
    "Good morning!",
    "Good afternoon!",
    "Good evening!",
    "Hi there!",
    "How's it going?",
    "Nice to meet you!",
    "What's up?",
    "Long time no see!",
    "Have a great day!"
]


# 3️⃣ 输出目录
output_dir = "C:\\Users\\33720\\Desktop\\temp\\tts"
os.makedirs(output_dir, exist_ok=True)

# 4️⃣ 遍历生成音频并播放
for i, word in enumerate(words, start=1):
    filename = os.path.join(output_dir, f"word_{i}.wav")

    # 生成语音文件
    tts.tts_to_file(text=word, file_path=filename)

    # # 播放音频
    # audio = AudioSegment.from_wav(filename)
    # play(audio)

    # 每个单词停 0.8 秒
    # time.sleep(0.8)

print("すべての単語を再生しました。")
